<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:insert="common.html"></div>

    <div class="zTreeDemoBackground left">
      <ul id="treeDemo" class="ztree"></ul>
    </div>
</body>
</html>
<script>
  var setting = {
    data: {
      simpleData: {
        enable: true
      }
    },
    async: {
      enable: true,
      url: "/menu/getZtreeMenu",
    },
    view : {
      addHoverDom: function(treeId, treeNode){
        var aObj = $("#" + treeNode.tId + "_a");
        if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
        var s = '<span id="btnGroup'+treeNode.tId+'">';
        if (treeNode.children == undefined) {
          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
        }else{
          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
          s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
        }
        s += '</span>';
        aObj.after(s);
      },
      removeHoverDom: function(treeId, treeNode){
        $("#btnGroup"+treeNode.tId).remove();
      }
    }
  };

  function addNode(parentid){
    BootstrapDialog.show({
      title: '增加',
      message: $('<div></div>').load('/menu/toAdd'),
      size: BootstrapDialog.SIZE_WIDE,
      buttons: [{
        label: '取消',
        action: function (dialog) {
          dialog.close();
        }
      }, {
        label: '确定',
        action: function (dialog) {
          /*var contentValue = CKEDITOR.instances.ccontent.getData();
          $("[name=ccontent]").val(contentValue);*/
          $.ajax({
            url : "/menu/add",
            type : "post",
            dataType : "json",
            data : $("#add_form").serialize()+"&parentid="+parentid,
            success : function (data){
              if (data == 1){
                toastr. success("增加成功");
                dialog.close();
                $.fn.zTree.init($("#treeDemo"), setting);
              }else{
                toastr. error("增加失败");
              }

            },
            error : function (data){
              alert("网络连接失败")
            }
          })
        }
      }]
    });
  }

  function editNode(parentid){
    BootstrapDialog.show({
      title: '修改',
      message: $('<div></div>').load('/menu/toUpdate?parentid='+parentid),
      size: BootstrapDialog.SIZE_WIDE,
      buttons: [{
        label: '取消',
        action: function (dialog) {
          dialog.close();
        }
      }, {
        label: '确定',
        action: function (dialog) {
          /*var contentValue = CKEDITOR.instances.ccontent.getData();
          $("[name=ccontent]").val(contentValue);*/
          $.ajax({
            url : "/menu/update",
            type : "post",
            dataType : "json",
            data : $("#update_form").serialize(),
            success : function (data){
              if (data == true){
                toastr. success("修改成功");
                dialog.close();
                $.fn.zTree.init($("#treeDemo"), setting);
              }else{
                toastr. error("修改失败");
              }

            },
            error : function (data){
              alert("网络连接失败")
            }
          })
        }
      }]
    });
  }

  function deleteNode(parentid){
    if(confirm("确认删除吗?")){
      $.ajax({
        url : "/menu/deleteInfo?parentid="+parentid,
        type : "post",
        dataType : "json",
        success : function (data){
          if (data == 1){
            toastr. success("删除成功");
            $.fn.zTree.init($("#treeDemo"), setting);
          }else{
            toastr. error("删除失败");
          }

        },
        error : function (data){
          alert("网络连接失败")
        }
      })
    }
  }

  $(document).ready(function(){
    $.fn.zTree.init($("#treeDemo"), setting);
  });
</script>